package org.dei.perla.sys.registry.datastructure;

import java.util.HashMap;

//import java.util.ArrayList;


/** Binary trees are used to store informations on the FPC currently instantiated in the systems.
 * for any attribute readable by an FPC(temperature, pressure, humidity.....) a binary tree is
 * instantiated. Each node of the tree has a key that represent the value of the attribute saved
 * a the node and the list of all FPC in the system that has 
 * */
public class BTreeData<T> implements Comparable<T>{

	private String attrName;	
	private FunctionalityProxyComponent fpcElement;
	private Comparable attrVal;
	
	public BTreeData(String attr_name){
		this.attrName = attr_name;
		fpcElement=null;
	}
	
	public BTreeData(String attr_name, FunctionalityProxyComponent fpc){
		this.attrName = attr_name;
		fpcElement= fpc;
		this.attrVal =fpc.getAttributesMap().get(attrName); 
	}
	
	public void setFPC(FunctionalityProxyComponent fpc){
		fpcElement = fpc;
		this.attrVal = fpc.getAttributesMap().get(attrName);
	}
	
	public FunctionalityProxyComponent getFPC(){
		return fpcElement;
	}
	
	
	public String getAttributeName(){
		return attrName;
	}
	
	
	public Comparable getAttrVal(){
		assert(this.fpcElement!=null):"no fpc element set in the btree_data ";
		return this.attrVal;
	}
	
	public int compareTo(Object o1) {
		
		if(!(o1 instanceof BTreeData<?>))throw new ClassCastException("BTreeData expected for comparison");
		BTreeData<?> bdata = (BTreeData<?>)o1;
		if(!attrName.equals(bdata.getAttributeName()))throw new InvalidTypeException("comparison on different attribute");

		Comparable attrVal = fpcElement.attributesMap.get(attrName);
		Comparable parAttrVal = ((BTreeData)o1).getFPC().getAttributesMap().get(attrName);
		
        return (attrVal.compareTo(parAttrVal));
		
   
    }
	/**
	 * 
	 * @param attrval the value of the attribute wrapped in the btree data structure
	 * @param attrtype, the type of the attribute stored at the node
	 * @return
	 */
	public static BTreeData getInstance(Comparable attrval, String attrtype){
    	HashMap<String,Comparable> attrMap = new HashMap<String,Comparable>();
		attrMap.put(attrtype, attrval);
		FunctionalityProxyComponent fpcTemp = new FunctionalityProxyComponent(attrMap);
    	BTreeData dataCur = new BTreeData(attrtype);
    	dataCur.setFPC(fpcTemp);
    	return dataCur;
	}
	
	
	
	
}
